const https = require('https');

export const getRiotData = async (url, key) => {
    const port = key.port
    const auth = key.token
    const headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': `Basic ${auth}`
    };
    const options = {
        hostname: '127.0.0.1',
        port: port,
        path: url,
        method: 'GET',
        headers: headers
    };
    return new Promise((resolve, reject) => {
        const req = https.request(options, (res) => {
            let data = '';
            res.on('data', (chunk) => {
                data += chunk;
            });
            res.on('end', () => {
                resolve(data);
            });
        });
        req.on('error', (error) => {
            reject(error);
        });
        req.end();
    })
};
export const postRiotData = async (url, data, method, all) => {
    const port = all.port
    const auth = all.token
    const headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': `Basic ${auth}`
    };
    const options = {
        hostname: '127.0.0.1',
        port: port,
        path: url,
        method: method,
        headers: headers
    };
    if (data != null) {
        // post请求携带的数据需要格式化
        const data1 = JSON.stringify(data)
        return new Promise((resolve, reject) => {
            const req = https.request(options, (res) => {
                let data = '';
                res.on('data', (chunk) => {
                    data += chunk;
                });
                res.on('end', () => {
                    resolve(data);
                });
            });
            req.on('error', (error) => {
                reject(error);
            });
            req.write(data1)
            req.end();
        })
    } else {
        return new Promise((resolve, reject) => {
            const req = https.request(options, (res) => {
                let data = '';
                res.on('data', (chunk) => {
                    data += chunk;
                });
                res.on('end', () => {
                    resolve(data);
                });
            });
            req.on('error', (error) => {
                reject(error);
            });
            req.end();
        })
    }

};

export const getImg = async (url, key) => {
    const port = key.port
    const auth = key.token
    const headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': `Basic ${auth}`
    };
    const options = {
        hostname: '127.0.0.1',
        port: port,
        path: url,
        method: 'GET',
        headers: headers
    };
    return new Promise((resolve, reject) => {
        const req = https.request(options, (res) => {
            var datas = [];
            var size = 0;
            res.on('data', function (data) {
                datas.push(data);
                size += data.length;
            })
            res.on('end', function (data) {
                var buff = Buffer.concat(datas, size);
                var pic = buff.toString('base64');
                resolve(pic);
            })
        });
        req.on('error', (error) => {
            reject(error);
        });
        req.end();
    })
};